//
//  CSViewUtils.swift
//  Cycling
//
//  Created by CSS on 3/30/23.
//

import Foundation

extension UIView {
    
    var screenScale: CGFloat {
        guard UIScreen.main.scale != 0 else {
            return 1
        }
        return 1 / UIScreen.main.scale
    }
    
    func cornerShaper(withRoundingCorners corners: UIRectCorner = .allCorners, radium: CGFloat = 12) {
        
        let bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: .init(width: radium, height: radium))
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = bezierPath.cgPath
        layer.mask = shapeLayer
        layer.masksToBounds = true
        
    }
    
    func addSegmentLine(onBottom bottom: Bool = true) {
        
        let lineView: UIView = .forAutoLayout()
        lineView.backgroundColor = .color(withHexString: "#D9D9D9")
        
        addSubview(lineView)
        
        var constraints = [
            lineView.leadingAnchor.constraint(equalTo: leadingAnchor),
            lineView.trailingAnchor.constraint(equalTo: trailingAnchor),
            lineView.heightAnchor.constraint(equalToConstant: screenScale)
        ]
        
        if bottom {
            constraints.append(lineView.bottomAnchor.constraint(equalTo: bottomAnchor))
        } else {
            constraints.append(lineView.topAnchor.constraint(equalTo: topAnchor))
        }
        
        NSLayoutConstraint.activate(constraints)
        
    }
    
}

// MARK: 工具类方法
extension UIView {
    class func forAutoLayout<ViewType: UIView>(frame: CGRect = .zero, hidden: Bool = false) -> ViewType {
        let view = ViewType.init(frame: frame)
        view.isHidden = hidden
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }
    
    func addSubviews(_ subviews: [UIView]) {
        subviews.forEach(addSubview(_:))
    }
    
    var safeTopAnchor: NSLayoutYAxisAnchor {
        if #available(iOS 11.0, *) {
            return safeAreaLayoutGuide.topAnchor
        }
        return topAnchor
    }
    
    var safeLeadingAnchor: NSLayoutXAxisAnchor {
        if #available(iOS 11.0, *) {
            return safeAreaLayoutGuide.leadingAnchor
        }
        return leadingAnchor
    }
    
    var safeBottomAnchor: NSLayoutYAxisAnchor {
        if #available(iOS 11.0, *) {
            return safeAreaLayoutGuide.bottomAnchor
        }
        return bottomAnchor
    }
    
    var safeRightAnchor: NSLayoutXAxisAnchor {
        if #available(iOS 11.0, *) {
            return safeAreaLayoutGuide.rightAnchor
        }
        return rightAnchor
    }
    
    var safeTrailingAnchor: NSLayoutXAxisAnchor {
        if #available(iOS 11.0, *) {
            return safeAreaLayoutGuide.trailingAnchor
        }
        return trailingAnchor
    }
    
    func constraintsForPinning(to parentView: UIView, respectingMargins margins: Bool = false) -> [NSLayoutConstraint] {
        let guide: Anchorable = (margins) ? parentView.layoutMarginsGuide : parentView
        
        let constraints = [
            topAnchor.constraint(equalTo: guide.topAnchor),
            leftAnchor.constraint(equalTo: guide.leftAnchor),
            bottomAnchor.constraint(equalTo: guide.bottomAnchor),
            rightAnchor.constraint(equalTo: guide.rightAnchor)
        ]
        return constraints
    }
    
    func pinTo(parentView parent: UIView, respectingMargins margins: Bool = false) {
        let constraints = constraintsForPinning(to: parent, respectingMargins: margins)
        NSLayoutConstraint.activate(constraints)
    }
    
}

protocol Anchorable {
    var topAnchor: NSLayoutYAxisAnchor { get }
    var bottomAnchor: NSLayoutYAxisAnchor { get }
    var leftAnchor: NSLayoutXAxisAnchor { get }
    var rightAnchor: NSLayoutXAxisAnchor { get }
    var leadingAnchor: NSLayoutXAxisAnchor { get }
    var trailingAnchor: NSLayoutXAxisAnchor { get }
}

extension UIView: Anchorable {}
extension UILayoutGuide: Anchorable {}
